package com.softdev.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.softdev.common.AbstractBaseEntity;
import io.swagger.annotations.ApiModel;
import lombok.*;
import org.hibernate.annotations.Proxy;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@ApiModel("用户")
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "users")
@JsonIgnoreProperties(value = { "authorities" })
public class User extends AbstractBaseEntity implements Serializable , UserDetails {

    private static final long serialVersionUID = 769415112642561292L;

    @Column(name = "username" , unique = true)
    private String username;

    @Column(name = "name")
    private String name;

    @Column(name = "phone")
    private String phone;

    @Column(name = "password")
    private String password;

    @Column(name = "avatar")
    private String avatar;

    //0：女 ， 1：男
    @Column(name = "sex")
    private int sex;

    @Column(name = "experience")
    private String experience;

    @ManyToMany(fetch = FetchType.EAGER , cascade = CascadeType.PERSIST)
    @JoinTable(name = "users_skills" ,
            joinColumns = {@JoinColumn(name = "user_id")} ,
            inverseJoinColumns = {@JoinColumn(name = "skill_id")})
    private List<Skill> skills;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY , cascade = CascadeType.PERSIST)
    @JoinTable(name = "users_roles" ,
            joinColumns = {@JoinColumn(name = "user_id")} ,
            inverseJoinColumns = {@JoinColumn(name = "role_id")})
    private List<Role> roles;

    @Column(name = "account_non_expired")
    private boolean accountNonExpired;

    @Column(name = "account_non_locked")
    private boolean accountNonLocked;

    @Column(name = "credentials_non_expired")
    private boolean credentialsNonExpired;

    @Column(name = "enabled")
    private boolean enabled;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role role : getRoles()) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    /**
     * @description: 判断是否未过期
     * @author: Jieyao Lu
     * @date: 2023/1/28  17:58
     * @param: []
     * @return: boolean
     **/
    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    /**
     * @description: 判断账户是否未锁定
     * @author: Jieyao Lu
     * @date: 2023/1/28  17:59
     * @param: []
     * @return: boolean
     **/
    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    /**
     * @description: 判断是否可以使用
     * @author: Jieyao Lu
     * @date: 2023/1/28  17:59
     * @param: []
     * @return: boolean
     **/
    @Override
    public boolean isEnabled() {
        return enabled;
    }

}
